<template>
  <div class="com-container">
    <div class="com-chart"
         ref="rankRef">

    </div>
  </div>
</template>

<script>
import { getRankData } from 'network/rank.js';
export default {
  name: 'Rank',
  data () {
    return {
      echartsInstance: null,
      allData: null, //服务器返回的数据
      startValue: 0, //区域缩放起始值
      endValue: 9, //区域缩放结束数值
      timer: null, //定时器标识
    }
  },
  mounted () {
    this.initChart();
    this.getData();
    this.adaptScreen();
    window.addEventListener('resize', this.adaptScreen);
  },
  destroyed () {
    window.removeEventListener('resize', this.adaptScreen);
    this.clearInterval(this.timer);
  },
  methods: {
    //初始化echarts实例化对象
    initChart () {
      this.echartsInstance = this.$echarts.init(this.$refs.rankRef, 'chalk');
      //对图表初始化配置的控制
      const initOption = {
        title: {
          text: '▎地区销量排行',
          left: 20,
          top: 20,
        },
        grid: {
          top: '18%',
          left: '3%',
          right: '6%',
          bottom: '5%',
          containLabel: true //grid 区域包含坐标轴的刻度标签
        },
        dataZoom: {
          type: 'slider',
          show: false,
        },
        tooltip: {
          show: true
        },
        xAxis: {
          type: 'category'
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            type: 'bar'
          }
        ]
      };
      this.echartsInstance.setOption(initOption);
      //鼠标移入，清除定时器
      this.echartsInstance.on('mouseover', () => {
        clearInterval(this.timer);
      })
      //鼠标移出，启用定时器
      this.echartsInstance.on('mouseout', () => {
        this.startInterval();
      })
    },

    //获取服务器的数据
    async getData () {
      const { data: res } = await getRankData();
      this.allData = res;
      console.log(this.allData);
      //从大到小排序
      this.allData.sort((a, b) => {
        return b.value - a.value //
      })
      this.updateChart();
      this.startInterval();
    },

    //更新图表
    updateChart () {
      const colorArr = [
        ['#0BA82C', '#4FF778'],
        ['#2E72BF', '#23E5E5'],
        ['#5052EE', '#AB6EE5']
      ]
      //所有省份名字形成的数组
      const provinceArr = this.allData.map(item => {
        return item.name;
      })
      //所有省份对应的销售金额
      const valueArr = this.allData.map(item => {
        return item.value;
      })
      const dataOption = {
        xAxis: {
          data: provinceArr
        },
        dataZoom: {
          startValue: this.startValue,
          endValue: this.endValue,
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: arg => {
                let targetColorArr = null;
                if (arg.value > 300) {
                  targetColorArr = colorArr[0];
                } else if (arg.value > 200) {
                  targetColorArr = colorArr[1];
                } else {
                  targetColorArr = colorArr[2];
                }
                //echart 渐变内置生成器echarts.graphic.LinearGradient
                return new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                  //百分之0状态下的颜色值
                  {
                    offset: 0,
                    color: targetColorArr[0]
                  },
                  //百分之100状态下的颜色值
                  {
                    offset: 1,
                    color: targetColorArr[1]
                  }
                ])
              }
            }
          }
        ]
      };
      this.echartsInstance.setOption(dataOption);

    },

    //浏览器窗口发生变化，调用
    adaptScreen () {
      const titleFontSize = this.$refs.rankRef.offsetWidth / 100 * 3.6;
      //和分辨率大小相关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              borderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0], //（顺时针左上，右上，右下，左下）
            }
          }
        ]
      };
      this.echartsInstance.setOption(adapterOption);
      this.echartsInstance.resize();
    },

    //关于区域缩放的定时器
    startInterval () {
      if (this.timer) {
        clearInterval(this.timer);
      }
      this.timer = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateChart();
      }, 3000);
    }


  },
}
</script>

<style lang="less" scoped>
</style>